package com.app.controller;

import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.MenuBo;
import com.app.domain.page.Page;
import com.app.domain.vo.MenuVo;
import com.app.framework.common.R;
import com.app.service.MenuService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单控制器
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 菜单列表
     */
    @PostMapping("/list")
    public R<Page<MenuVo>> lists(@RequestBody MenuBo bo) {
        return R.ok(menuService.lists(bo));
    }

    /**
     * 菜单详情
     *
     * @param menuId 菜单ID
     */
    @GetMapping("/details/{menuId}")
    public R<MenuVo> details(@PathVariable("menuId") Long menuId) {
        return R.ok(menuService.details(menuId));
    }

    /**
     * 添加菜单
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("save_menu")
    @PostMapping("/save")
    public R<?> saveOne(@RequestBody MenuBo bo) {
        menuService.saveOne(bo);
        return R.ok(null, "创建成功");
    }

    /**
     * 修改菜单
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("update_menu")
    @PostMapping("/update")
    public R<?> updateOne(@RequestBody MenuBo bo) {
        Assert.notNull(bo.getId(), "菜单ID不能为空");
        menuService.updateOne(bo);
        return R.ok(null, "修改成功");
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     */
    @HasRole()
    @HasRedissonLock("delete_menu")
    @PostMapping("/delete/{menuId}")
    public R<?> deleteOne(@PathVariable("menuId") Long menuId) {
        menuService.deleteOne(menuId);
        return R.ok(null, "删除成功");
    }

}